## Table H.1: Firm and Employee Donations and Lobbying of Specific Politicians, 
#First-Time Donation in 3 Years, Manufacturing Firms, Full Results--------------

## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to main folder
# Step 2: Adjust DATA_DIR to where data is is located
# Step 3: Adjust FIG_DIR to where output folder is
# Step 4: Run entire script
## setup -----------------------------------------------------------------------



## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())

# load packages
library(dplyr)
library(stargazer)
library(lfe)
library(tidyr)
options(scipen=999)

# set directory
MAIN_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package"

# set data folder
DATA_DIR <-  paste(MAIN_DIR, "/data/", sep = "")

# set output folder
FIG_DIR <- paste(MAIN_DIR, "/output/", sep = "")


#subset to manufacturing industries
naics_sub <- c("31", "32", "33") # set industries to be analyzed

# file to be read in
#file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.csv") # file to be input
file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.rds") # file to be input


# read data
don_lob <- readRDS(file_in) %>%
  filter(naics2 %in% naics_sub) %>%
  mutate(l_sale = log(sale+1), 
         l_emp = log(emp+1), 
         l_don_tot = log(don_tot+1)) %>%
  select(gvkey_govtrack_id, gvkey, govtrack_id, year, naics, naics3, naics2, chamber, 
         lob_any_bin, lob_spon_bin, lob_cosp_bin, lob_comm_bin,
         don_any_bin, treat_don_all,
         don_tot, l_don_tot,
         l_sale, l_emp, # firm covariates
         majority, democrat, com_chair, power_com) %>% # recodings
  mutate(gvkey_govtrack_id_numeric = as.integer(as.factor(gvkey_govtrack_id)),
         year = as.integer(year),
         l_treat_don_all = ifelse(treat_don_all == 1, l_don_tot, 0)) 

gc()
gc()

# create treatment indicators, first donation within 3-years
don_lob <- don_lob %>%
  arrange(gvkey, govtrack_id, year) %>%
  group_by(gvkey, govtrack_id) %>%
  mutate(treat_don_all_3y = ifelse(don_tot > 0 & lag(don_tot, 1) == 0 & lag(don_tot, 2) == 0 & lag(don_tot, 3) == 0, 1, 0)) %>%
  mutate(l_treat_don_all_3y = ifelse(treat_don_all_3y == 1, l_don_tot, 0)) %>%
  replace_na(list(treat_don_all_3y = 0, l_treat_don_all_3y = 0))
gc()



# Regression models, binary donation indicator, first donation within 3 years
lm1 <- lfe::felm(lob_any_bin ~ treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm2 <- lfe::felm(lob_any_bin ~ treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm3 <- lfe::felm(lob_any_bin ~ treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey + year + govtrack_id | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm4 <- lfe::felm(lob_any_bin ~ treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey_govtrack_id + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
gc()



# Regression models, logged donation account, first donation within 3 years
lm5 <- lfe::felm(lob_any_bin ~ l_treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm6 <- lfe::felm(lob_any_bin ~ l_treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm7 <- lfe::felm(lob_any_bin ~ l_treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey + year + govtrack_id | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm8 <- lfe::felm(lob_any_bin ~ l_treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey_govtrack_id + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
gc()


# create and output table
# table output with all covariates included, for appendix
stargazer::stargazer(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8, type = "latex", 
                     title = "\\\textbf{Firm and Employee Donations and Lobbying of Specific Politicians, First-Time Donation in 3 Years, Manufacturing Firms, Full Results:} The level of analysis is the firm-politician-year, and the data include all publicly traded manufacturing firms (NAICS 2-digit codes 31-33) and members of Congress between 2008 and 2018. The dependent variable is a dummy which equals 1 if firm $i$ lobbied the legislative activity of a politician $j$ in year $t$. $Donationt$ equals 1 if firm $i$ donated to a politician $j$ for the first time in 3 years $t$, and 0 otherwise, whereas $Donation Amount_{t}$ is the logged amount of the 3-year first-time donation. The coefficients come from ordinary least squares regressions, using combinations of firm, politician, year, and firm-politician fixed effects. All models control for politician partisanship, majority status, committee chairmanship, and being member of the top 4 powerful committees. The standard errors are clustered by firm.", 
                     label = "tab_h1",
                     out = paste0(FIG_DIR, "table_h1.tex"),
                     covariate.labels = c("$Donation_{t}$","$Donation\\;Amount_{t}$",
                                          "$Majority_{t}$", "$Democrat_{t}$", 
                                          "$Committee\\;Chair_{t}$", "$Powerful\\;Committee_{t}$"), 
                     dep.var.labels = c("Lobbying Legislative Activity$_{t}$"),
                     add.lines = list(
                       #c("Politician Controls",  rep(c("$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"), 2)),
                       c("Firm FEs",  rep(c("$\\checkmark$", "$\\checkmark$", "$\\checkmark$", ""), 2)),
                       c("Year FEs",  rep(c("", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"), 2)),
                       c("Politician FEs",rep(c("", "", "$\\checkmark$", ""), 2)),
                       c("Firm-Politician FEs",rep(c("", "", "", "$\\checkmark$"), 2))),
                     omit.stat = c("rsq", "ser"),
                     # keep = c("treat_don_all", "l_treat_don_all"),
                     column.sep.width = "3pt", notes.align = "l")

rm(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8)
gc()
